/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.service;

import com.bice.dao.Dao;
import com.bice.entity.Post;
import com.bice.entity.User;
import com.bice.util.FacesUtil;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

/**
 *
 * @author sirius
 */
@Service
public class PostService extends GenericService<Post> {
    
    @Resource
    private Dao<Post, Serializable> dao;

    public PostService() {
    }
    
    @Override
    public void save(Post post) {
        if(post.getComment() != null) post.setComment(post.getComment().trim());
        dao.save(post);
    }
    
    public List<Post> getConversationDate(User postTo, User from) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Post.class);        
        criteria.add(Restrictions.or(
                Restrictions.and(
                    Restrictions.eq("user", postTo),
                    Restrictions.eq("fromUser", from)),
                Restrictions.and(
                    Restrictions.eq("fromUser", postTo),
                    Restrictions.eq("user", from))));
                
        criteria.addOrder(Order.asc("created"));
        List<Post> posts = dao.get(criteria);
        System.out.println(from.getNickname() + " - " + posts.size());
        return posts;
    }
    
    public List<Post> getPostByCreatedDate(Post parent) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Post.class);        
        criteria.add(Restrictions.eq("parent", parent));
        criteria.addOrder(Order.asc("created"));
        return dao.get(criteria);
    }
    
}   